package edu.gdin.ecommerce.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import edu.gdin.ecommerce.dao.DicDetailDAO;
import edu.gdin.ecommerce.dao.DicTypeDAO;
import edu.gdin.ecommerce.model.DicDetail;
import edu.gdin.ecommerce.model.DicType;
import edu.gdin.ecommerce.model.ParamDetail;
import edu.gdin.ecommerce.model.ParamType;
import edu.gdin.ecommerce.service.IDicService;
import edu.gdin.ecommerce.util.Pager;

@Component("dicService")
public class DicService implements IDicService {

	private DicTypeDAO dicTypeDAO;
	private DicDetailDAO dicDetailDAO;
	
	public DicTypeDAO getDicTypeDAO() {
		return dicTypeDAO;
	}

	@Resource(name="dicTypeDAO")
	public void setDicTypeDAO(DicTypeDAO dicTypeDAO) {
		this.dicTypeDAO = dicTypeDAO;
	}
	
	
	public DicDetailDAO getDicDetailDAO() {
		return dicDetailDAO;
	}

	@Resource(name="dicDetailDAO")
	public void setDicDetailDAO(DicDetailDAO dicDetailDAO) {
		this.dicDetailDAO = dicDetailDAO;
	}

//	@Override
	public Pager queryDicType(String tName, String tDesc, Integer cPage) {
		Pager pager = new Pager();
		pager = dicTypeDAO.queryDicTypeByNameOrDesc(tName, tDesc, cPage);
		return pager;
	}
	

//	@Override
	public boolean addDicType(DicType dType) {
		if (!dicTypeDAO.isExistByEntityName("DicType", "name", dType.getName()))
			return dicTypeDAO.saveDicType(dType);
		else
			return false;
	}



//	@Override
	public Pager queryDicDetail(DicDetail dDetail, Integer cPage) {
		
		Pager pager = new Pager();
		
		pager = dicDetailDAO.queryDicDetail(dDetail , cPage, pager.getPageSize());
		
		return pager;
	}
	
	public List<DicType> queryAllTypes(){
		return (List<DicType>) dicTypeDAO.queryAllTypes();
	}
	

	public DicType queryDicTypeById(String id) {
		return (DicType) dicTypeDAO.findByPk(DicType.class, id);
	}

	public boolean addDicDetail(DicDetail pd) {
		try{
			dicDetailDAO.insert(pd);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	
	public List<DicDetail> getAllDicDetails() {
		return (List<DicDetail>) this.dicDetailDAO.list(DicDetail.class);
	}

	@Override
	public Pager queryDicDetailByTypeId(String dicTypeId, Integer cPage) {
		return this.dicDetailDAO.queryDIcDetailByDicTypeId(dicTypeId, cPage);
	}
}
